import { widgetManager } from '@/common/managers';
import { NMMeshBaseCommand } from '@/common/types';
import { nmAppLog } from '@/common/utils';
import i18n from '@/common/utils/i18next';
import { mesh } from '@/domain';
import { IChangeEvent } from '@rjsf/core';
import { CmdDialogForm } from 'nextcae-base';
import { useEffect, useState } from 'react';
import { MeshDialogProps } from '../type';
import { schema, uiSchema } from './schema';
import { useFocusStore } from 'nextcae-base/src/common/store';
import { PartMaterialField } from '@/components/PartMaterial';

// 扫掠体网格
function SweepMeshDialog({ title, onClose }: MeshDialogProps) {
  const [hintMessage] = useState('进入扫掠体网格命令,请选择面');
  const [isLoading, setIsLoading] = useState(false);
  const [formData, setFormData] = useState<any>({
    Selection: {
      Source: [],
      Target: [],
      Domain: [],
    },
    MeshSize: {
      Size: null,
    },
  });
  const { reset } = useFocusStore();
  useEffect(() => {
    widgetManager.hint(hintMessage);
    return () => {
      widgetManager.hint();
    };
  }, [hintMessage]);

  const handleSubmit = async (data) => {
    nmAppLog('handleSubmit====>:', data);
    // return MessageTip.warning('请拾取Source面！');
    if (data) {
      const { Selection, MeshSize } = data;

      setIsLoading(true);
      const params = {
        EntityType: 'Face',
        Domain: Selection.Domain[0],
        Source: Selection.Source,
        Target: Selection.Target,
        Size: MeshSize.Size,
        // Number: MeshSize.Number,
        Part: data.AddTo.Part,
        Property: data.AddTo.Property,
      };
      // if (MeshSize.Method === 'BySize') {
      //   params['Size'] = MeshSize.Size;
      //   delete params['Number'];
      // } else {
      //   params['Number'] = MeshSize.Number;
      //   delete params['Size'];
      // }
      try {
        await mesh.sweepMesh(params);
        setFormData((state) => ({
          ...state,
          Selection: {
            Source: [],
            Target: [],
            Domain: [],
          },
        }));
        reset();
      } catch (error) {
        nmAppLog(error);
      } finally {
        setIsLoading(false);
      }
      nmAppLog('handleSubmit:', data, params);
    }
  };
  const hanldeChange = (data: IChangeEvent, id) => {
    nmAppLog('handleResultSelectChange:', data, id);
    if (!id) return;
    const keys = id.split('_');
    if (data.formData) {
      setFormData((state) => ({
        ...data.formData,
        [keys[1]]: { ...state[keys[1]], ...data.formData[keys[1]] },
      }));
    }
  };

  return (
    <CmdDialogForm
      title={title}
      formData={formData}
      titleIcon={cmdSweepMesh.icon}
      onSubmit={handleSubmit}
      submitLoading={isLoading}
      onChange={hanldeChange}
      schema={schema}
      extraUiSchema={uiSchema}
      fields={{
        partMaterialField: PartMaterialField,
      }}
      onClose={onClose}></CmdDialogForm>
  );
}

export const cmdSweepMesh: NMMeshBaseCommand = {
  id: 'cmdSweepMesh',
  icon: 'commands/mesh/sweep-mesh',
  label: 'command.mesh.sweepMesh',
  create: (label: string, onClose: () => void) => (
    <SweepMeshDialog
      title={i18n.t(label)}
      onClose={onClose}
    />
  ),
};
